package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.tms.entity.misc.DeliveryNote;
import cn.buk.tms.entity.misc.OperationLog;
import cn.buk.tms.sc.SearchFlightOrderCriteria;
import cn.buk.tms.delivery.dto.DeliveryNoteDto;
import cn.buk.tms.delivery.service.DeliveryNoteService;
import java.util.Date;
import java.util.List;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author yfdai
 */
@RestController
@RequestMapping("/delivery")
public class DeliveryNoteController extends BaseController {

    @Autowired
    private DeliveryNoteService service;

    /**
     * 创建配送单
     * @param request
     * @return
     */
    @PostMapping(value = "/note")
    public JsonResult createDeliveryNote(HttpServletRequest request,
        @RequestParam(value = "deliveredOrderNo", required = false) final String deliveredOrderNo,
        @RequestParam(value = "deliveryContent", required = true) final String deliveryContent,
        @RequestParam(value = "deliveryDate", required = false) final Date deliveryDate,
        @RequestParam(value = "receiver", required = false) final String receiver,
        @RequestParam(value = "mobile", required = true) final String mobile,
        @RequestParam(value = "address", required = true) final String address,
        @RequestParam(value = "remark", required = false) final String remark
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        DeliveryNote dn = new DeliveryNote();
        dn.setEnterpriseId(enterpriseId);

        dn.setDeliveredOrderNo(deliveredOrderNo);
        if (deliveredOrderNo != null && deliveredOrderNo.trim().length() > 4) {
            dn.setDeliveredOrderType(deliveredOrderNo.trim().substring(0, 4));
        }

        dn.setDeliveryDate(deliveryDate);
        dn.setDeliveredContent(deliveryContent);

        dn.setReceiver(receiver);
        dn.setMobile(mobile);
        dn.setAddress(address);

        dn.setRemark(remark);

        return service.createDeliveryNote(enterpriseId, username, dn);
    }

    /**
     * 搜索配送单
     * @param request
     * @param sc
     * @return
     */
    @RequestMapping("/notes")
    public CommonDto<DeliveryNote> searchDeliveryNotes(HttpServletRequest request,
                                                    @ModelAttribute("sc") SearchFlightOrderCriteria sc) {
        final int enterpriseId = getEnterpriseId(request);

        List<DeliveryNote> datas = service.searchDeliveryNotes(enterpriseId, sc);

        CommonDto<DeliveryNote> commonDto = new CommonDto<>();
        commonDto.setDataList(datas);
        commonDto.setPage(sc.getPage());

        return commonDto;
    }

    /**
     * 获取配送单详情
     * @param request
     * @return
     */
    @RequestMapping("/note/{orderId}")
    public DeliveryNoteDto searchHotelOrders(HttpServletRequest request,
                                        @PathVariable("orderId") final int orderId) {
        final int enterpriseId = getEnterpriseId(request);

        return service.getDeliveryNoteById(enterpriseId, orderId);
    }

    /**
     * 修改派送日期
     */
    @PostMapping(value = "/note/{orderId}/deliveryDate")
    public JsonResult updateFlightOrderDeliveryDate(HttpServletRequest request,
        @PathVariable("orderId") int orderId,
        @RequestParam("deliveryDate") Date deliveryDate
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.updateDeliveryNoteDeliveryDate(enterpriseId, username, orderId, deliveryDate);
    }

        /**
     * 开始处理配送单
     * @param request
     * @param orderId
     * @return
     */
    @PostMapping(value = "/note/{orderId}/status/processing")
    public JsonResult processDeliveryNote(HttpServletRequest request,
                                                     @PathVariable("orderId") final int orderId) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.processDeliveryNote(enterpriseId, username, orderId);
    }

        /**
     * 处理完毕
     * @param request
     * @param orderId
     * @param remark
     * @return
     */
    @PostMapping(value = "/note/{orderId}/status/finished")
    public JsonResult finishDeliveryNote(HttpServletRequest request,
                                        @PathVariable("orderId") final int orderId,
                                       @RequestParam("remark") String remark) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.finishDeliveryNote(enterpriseId, username, orderId, remark);
    }

    @PostMapping(value = "/note/{orderId}/status/canceled")
    public JsonResult cancelDeliveryNote(HttpServletRequest request,
                                       @PathVariable("orderId") final int orderId,
                                       @RequestParam("remark") String remark) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.cancelDeliveryNote(enterpriseId, username, orderId, remark);
    }

    @RequestMapping(value = "/note/{orderId}/history")
    public List<OperationLog> searchDeliveryNoteHistory(HttpServletRequest request,
        @PathVariable("orderId") final int orderId) {
        final int enterpriseId = getEnterpriseId(request);

        return service.searchDeliveryNoteHistory(enterpriseId, orderId);
    }


}
